import { Dialog, ConfigProvider } from '@teamix/ui';
import ReactDOM from 'react-dom';

let isToastExist = false;
export default function toastErrorOnce(toastConfig) {
  if (typeof toastConfig !== 'object') {
    return;
  }
  if (!isToastExist) {
    isToastExist = true;

    // 设置 Dialog.alert 的 ConfigProvider
    const element = document.createElement('div');
    element.id = 'ConfigProvider-setter';
    document.body.insertBefore(element, document.body.firstChild);
    ReactDOM.render(
      <ConfigProvider prefix="yx-">
        <div />
      </ConfigProvider>,
      document.getElementById('ConfigProvider-setter'),
    );

    Dialog.alert({
      ...toastConfig,
      onOk: () => {
        window.location.reload();
      },
      onClose: () => {
        window.location.reload();
        return false;
      },
    });
  }
}
